﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Cashier
{
    class Customer
    {
        public int id;
        public string card_id;
        public string full_name;
        public bool gender;
        public string phone;
        public string address;
        public DateTime join_date;
        public DateTime birthday;
        public DateTime expire_date;
        public int discount_percent;
        public int point;
        public bool is_vip;
        public int revision;
 
        public bool is_deleted;
        public AccessDB accessdb;
        public MySQLDB mysqldb;

        public Customer(AccessDB _accessdb, MySQLDB _mysqldb) 
        {
            accessdb = _accessdb;
            mysqldb = _mysqldb;
        }

        public bool addPoint(int _point)
        {
            DataSet ds = accessdb.ExeQuery("UPDATE customer SET point = point + "+_point+", revision = revision + 1 where card_id = '" + card_id + "'");
            return true;
        }

        public void setVIP()
        {
            DataSet ds = accessdb.ExeQuery("UPDATE customer SET is_vip = Yes, discount_percent = 10 where card_id = '" + card_id + "'");
        }

        public bool RenewMembership()
        {
            if (accessdb.ExeNonQuery("UPDATE customer SET expire_date = DateAdd('yyyy',1,Date()),point=0, revision = revision + 1 where card_id = '" + card_id + "'") > 0)
            {
                return true;
            }
            return false;
        }

        public bool getCustomerLocalByCardID(string CardID)
        {
            
            DataSet ds = accessdb.ExeQuery("Select * from customer where card_id = '" + CardID + "'");
            if (ds.Tables[0].Rows.Count != 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                        
                    // Parse Data
                    try
                    {
                        if (dr["id"].ToString() != "")
                        {
                            id = int.Parse(dr["id"].ToString());
                        }

                        if (dr["card_id"].ToString() != "")
                        {
                            card_id = dr["card_id"].ToString();
                        }

                        if (dr["full_name"].ToString() != "")
                        {
                            full_name = dr["full_name"].ToString();
                        }

                        if (dr["gender"].ToString() != "")
                        {
                            gender = bool.Parse(dr["gender"].ToString());
                        }

                        if (dr["phone"].ToString() != "")
                        {
                            phone = dr["phone"].ToString();
                        }

                        if (dr["address"].ToString() != "")
                        {
                            address = dr["address"].ToString();
                        }

                        if (dr["join_date"].ToString() != "")
                        {
                            join_date = DateTime.Parse(dr["join_date"].ToString());
                        }

                        if (dr["expire_date"].ToString() != "")
                        {
                            expire_date = DateTime.Parse(dr["expire_date"].ToString());
                        }

                        if (dr["birthday"].ToString() != "")
                        {
                            birthday = DateTime.Parse(dr["birthday"].ToString());
                        }

                        if (dr["discount_percent"].ToString() != "")
                        {
                            discount_percent = int.Parse(dr["discount_percent"].ToString());
                        }

                        if (dr["point"].ToString() != "")
                        {
                            point = int.Parse(dr["point"].ToString());
                        }

                        if (dr["is_vip"].ToString() != "")
                        {
                            is_vip = bool.Parse(dr["is_vip"].ToString());
                        }

                        if (dr["revision"].ToString() != "")
                        {
                            revision = int.Parse(dr["revision"].ToString());
                        }

                        
                    }
                    catch (Exception Ex)
                    {

                    }
                }
                return true;
            }
            return false;
        }

        
        public List<Customer> getCustomerLocalListByCondition(string Condition)
        {
            List<Customer> CustomerList = new List<Customer>();
            DataSet ds = accessdb.ExeQuery("Select * from customer where 1 and " + Condition);
            if (ds.Tables[0].Rows.Count != 0)
            {
                foreach (DataTable table in ds.Tables)
                {
                    foreach (DataRow dr in table.Rows)
                    {
                        Customer tmpCustomer = new Customer(accessdb,mysqldb);
                        // Parse Data
                        try
                        {
                            if (dr["id"].ToString() != "")
                            {
                                tmpCustomer.id = int.Parse(dr["id"].ToString());
                                // = int.Parse(dr["id"].ToString());
                            }

                            if (dr["card_id"].ToString() != "")
                            {
                                tmpCustomer.card_id = dr["card_id"].ToString();
                            }

                            if (dr["full_name"].ToString() != "")
                            {
                                tmpCustomer.full_name = dr["full_name"].ToString();
                            }

                            if (dr["gender"].ToString() != "")
                            {
                                tmpCustomer.gender = bool.Parse(dr["gender"].ToString());
                            }

                            if (dr["phone"].ToString() != "")
                            {
                                tmpCustomer.phone = dr["phone"].ToString();
                            }

                            if (dr["address"].ToString() != "")
                            {
                                tmpCustomer.address = dr["address"].ToString();
                            }

                            if (dr["join_date"].ToString() != "")
                            {
                                tmpCustomer.join_date = DateTime.Parse(dr["join_date"].ToString());
                            }

                            if (dr["expire_date"].ToString() != "")
                            {
                                tmpCustomer.expire_date = DateTime.Parse(dr["expire_date"].ToString());
                            }

                            if (dr["birthday"].ToString() != "")
                            {
                                tmpCustomer.birthday = DateTime.Parse(dr["birthday"].ToString());
                            }

                            if (dr["discount_percent"].ToString() != "")
                            {
                                tmpCustomer.discount_percent = int.Parse(dr["discount_percent"].ToString());
                            }

                            if (dr["point"].ToString() != "")
                            {
                                tmpCustomer.point = int.Parse(dr["point"].ToString());
                            }

                            if (dr["is_vip"].ToString() != "")
                            {
                                tmpCustomer.is_vip = bool.Parse(dr["is_vip"].ToString());
                            }

                            if (dr["revision"].ToString() != "")
                            {
                                tmpCustomer.revision = int.Parse(dr["revision"].ToString());
                            }

                            CustomerList.Add(tmpCustomer);
                        }
                        catch (Exception Ex)
                        {

                        }
                    }
                }
            }
            return CustomerList;
        }

        public List<Customer> getCustomerOnlineList()
        {
            List<Customer> CustomerList = new List<Customer>();
    
            DataTable dt = mysqldb.ExecuteQuery("Select * from CUSTOMER where revision > 0");
            if (dt.Rows.Count != 0)
            {
                    foreach (DataRow dr in dt.Rows)
                    {
                        Customer tmpCustomer = new Customer(accessdb,mysqldb);
                        // Parse Data
                        try
                        {
                            if (dr["id"].ToString() != "")
                            {
                                tmpCustomer.id = int.Parse(dr["id"].ToString());
                                // = int.Parse(dr["id"].ToString());
                            }

                            if (dr["card_id"].ToString() != "")
                            {
                                tmpCustomer.card_id = dr["card_id"].ToString();
                            }

                            if (dr["full_name"].ToString() != "")
                            {
                                tmpCustomer.full_name = dr["full_name"].ToString();
                            }

                            if (dr["gender"].ToString() != "")
                            {
                                tmpCustomer.gender = bool.Parse(dr["gender"].ToString());
                            }

                            if (dr["phone"].ToString() != "")
                            {
                                tmpCustomer.phone = dr["phone"].ToString();
                            }

                            if (dr["address"].ToString() != "")
                            {
                                tmpCustomer.address = dr["address"].ToString();
                            }

                            if (dr["join_date"].ToString() != "")
                            {
                                tmpCustomer.join_date = DateTime.Parse(dr["join_date"].ToString());
                            }

                            if (dr["expire_date"].ToString() != "")
                            {
                                tmpCustomer.expire_date = DateTime.Parse(dr["expire_date"].ToString());
                            }

                            if (dr["birthday"].ToString() != "")
                            {
                                tmpCustomer.birthday = DateTime.Parse(dr["birthday"].ToString());
                            }

                            if (dr["discount_percent"].ToString() != "")
                            {
                                tmpCustomer.discount_percent = int.Parse(dr["discount_percent"].ToString());
                            }

                            if (dr["point"].ToString() != "")
                            {
                                tmpCustomer.point = int.Parse(dr["point"].ToString());
                            }

                            if (dr["is_vip"].ToString() != "")
                            {
                                tmpCustomer.is_vip = bool.Parse(dr["is_vip"].ToString());
                            }

                            if (dr["revision"].ToString() != "")
                            {
                                tmpCustomer.revision = int.Parse(dr["revision"].ToString());
                            }

                            CustomerList.Add(tmpCustomer);
                        }
                        catch (Exception Ex)
                        {

                        }
                    }
                
            }
            return CustomerList;
        }

        public bool UpdateCustomerLocalData() 
        {
            if (full_name != null)
            {
                full_name = full_name.Replace("'", " ");
            }
            if (phone != null)
            {
                phone = phone.Replace("'", " ");
            }
            if (address != null)
            {
                address = address.Replace("'", " ");
            }
            if (accessdb.ExeNonQuery("UPDATE CUSTOMER SET full_name='" + full_name + "', gender=" + gender + ", phone='" + phone + "', address='" + address + "', join_date='" + join_date.ToString("d") + "', expire_date='" + expire_date.ToString("d") + "', birthday='" + birthday.ToString("d") + "', discount_percent=" + discount_percent + ", point=" + point + ", is_vip=" + is_vip + ", revision=" + revision + " WHERE card_id= '" + card_id + "'") > 0)
            {
                return true;
            }
            return false;
        }

        public bool AddCustomerLocal()
        {

            if (accessdb.ExeNonQuery("INSERT INTO CUSTOMER(card_id,full_name, gender, phone, address, join_date, expire_date, birthday, discount_percent, point, is_vip, revision) values('" + card_id + "','" + full_name + "'," + gender + ",'" + phone + "','" + address + "','" + join_date.Year + "/" + join_date.Month + "/" + join_date.Day + "','" + expire_date.Year + "/" + expire_date.Month + "/" + expire_date.Day + "','" + birthday.Year + "/" + birthday.Month + "/" + birthday.Day + "'," + discount_percent + "," + point + "," + is_vip + "," + revision + ") ") > 0)
            {
                return true;
            }
            
            return false;
        }

        public bool UpdateCustomerLocalFromServer() 
        {
            Customer customerserver = new Customer(accessdb, mysqldb);
            
            List<Customer> listcustomerserver = new List<Customer>();
            listcustomerserver = customerserver.getCustomerOnlineList();
            foreach (Customer _customerserver in listcustomerserver)
            {
                Customer customerlocalcheck = new Customer(accessdb, mysqldb);
                if (customerlocalcheck.getCustomerLocalByCardID(_customerserver.card_id)) //UPDATE
                {
                    _customerserver.UpdateCustomerLocalData();
                }
                else //ADDNEW
                {
                    _customerserver.AddCustomerLocal();
                }
            }
            return true;
        }
    }
}
